<?php session_start(); ?>
<?php
require_once '../../ptnhuan-web1/libraries/utilites/functions.php';
$path = "../../ptnhuan-web1/data/films.txt";
$full_path = realpath($path);
$films = mreadFile($path);
$rating1 = NULL;
$rating2 = NULL;
?>
<?php if (isset($_GET["logout"]) && $_GET["logout"] == true) { ?>
    <?php unset($_SESSION["id"]); ?>
    <?php header("location:login.php"); ?>
<?php } ?>
<div align="right"><a href="file4.php?logout=true">&nbsp;&nbsp; Logout </a></div>
<br>
<div align="center">
    <form method="POST">
        From Rating:<input type="text" name="rating1">
        To Rating:<input type="text" name="rating2">
        <input type="submit" name="submit" value="Search">
    </form>
</div>
<?php if (isset($_POST['submit'])): ?>
    <?php $rating1 = (double) $_POST['rating1']; ?>
    <?php $rating2 = (double) $_POST['rating2']; ?>
<?php endif; ?>
<?php if (isset($_SESSION['id'])) { ?>
    <?php foreach ($films as $film): ?>
        <?php
        $film = trim($film);
        if (!empty($film)):
            ?>
            <?php $mfil = explode(";", $film); ?>
            <?php if (empty($rating1) && empty($rating2)): ?>
                <?php if (isset($mfil[2]) || isset($mfil[3])): ?>
                    <?php if (isset($_GET['uid'])): ?>
                        <?php if ((int) $_GET['uid'] != 0): ?>
                            <?php if ((int) $_GET['uid'] == (int) $mfil[4]): ?>
                                <?php echo $mfil[0] ?>
                                <img src='../../ptnhuan-web1//public/images/films/<?php echo $mfil[1] ?>'>
                                <?php echo $mfil[2] ?>
                                <?php echo $mfil[3] ?>
                                <?php echo $mfil[4] ?>
                                <a href=edit.php?if=<?php echo $mfil[0]; ?>>edit</a><br>
                            <?php endif; ?>     
                        <?php else : ?> 
                            <img src='../../ptnhuan-web1//public/images/films/<?php echo $mfil[1] ?>'>
                            <?php echo $mfil[2] ?>
                            <?php echo $mfil[3] ?>
                            <?php echo $mfil[4] ?>
                            <a href=edit.php?if=<?php echo $mfil[0]; ?>>edit</a><br>    
                        <?php endif; ?>  
                    <?php endif; ?> 
                <?php endif; ?>  
            <?php else : ?>
                <?php if (isset($mfil[2]) || isset($mfil[3])): ?>
                    <?php if (($rating1 <= (double) $mfil[3]) && ($rating2 >= (double) $mfil[3])): ?>
                        <?php if (isset($_GET['uid'])): ?>
                            <?php if ((int) $_GET['uid'] != 0): ?>
                                <?php if ((int) $_GET['uid'] == (int) $mfil[4]): ?>
                                    <?php echo $mfil[0] ?>
                                    <img src='../../ptnhuan-web1//public/images/films/<?php echo $mfil[1] ?>'>
                                    <?php echo $mfil[2] ?>
                                    <?php echo $mfil[3] ?>
                                    <?php echo $mfil[4] ?>
                                    <a href=edit.php?if=<?php echo $mfil[0]; ?>>edit</a><br>
                                <?php endif; ?>     
                            <?php else : ?>
                                <img src='../../ptnhuan-web1//public/images/films/<?php echo $mfil[1] ?>'>
                                <?php echo $mfil[2] ?>
                                <?php echo $mfil[3] ?>
                                <?php echo $mfil[4] ?>
                                <a href=edit.php?if=<?php echo $mfil[0]; ?>>edit</a><br>    
                            <?php endif; ?>  
                        <?php endif; ?> 
                    <?php endif; ?>  
                <?php endif; ?>  
            <?php endif; ?> 
        <?php endif; ?> 
    <?php endforeach; ?>
<?php }else { ?>
    <?php header("location:login.php"); ?>           
<?php } ?>




